Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto bind component data-test-* attributes #27

Merged
merged 8 commits into from
Jan 11, 2017

Conversation

marcoow
Copy link
Member

@marcoow marcoow commented Dec 2, 2016

This is the follow-up PR for #26.

Instead of automatically inserting data-test-* attributes for all components with the component's internal name as a value we're now simply auto-binding all data-test-* attributes that are present on a component.

@marcoow
Copy link
Member Author

marcoow commented Dec 2, 2016

@Turbo87: the preprocessor that strips data-test-* attributes from components' JS and the assignments in the templates might be sth. for you to look into in Jan.

@@ -1,6 +1,5 @@
/*jshint node:true*/
module.exports = {
useVersionCompatibility: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcoow was this an intentional change? as discussed earlier today we should most likely test all minor Ember releases since we're using an Handlebars AST transform here and the AST is not public/stable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this was just a result of running ember init after updating Ember CLI.

# Usually, it's ok to finish the test scenario without reverting
# to the addon's original dependency state, skipping "cleanup".
- ember try:one $EMBER_TRY_SCENARIO test --skip-cleanup
- npm run mocha
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is failing because there is no mocha script defined. what's the reason for using mocha if all you do is check JSHint compliance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, don't remember to be honest 😊

"ember-resolver": "^2.0.3",
"loader.js": "^4.0.1",
"ember-welcome-page": "^1.0.3",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is most likely not needed here

"ember-cli-babel": "^5.1.5",
"lodash": "^4.0.0"
"ember-cli-babel": "^5.1.7",
"lodash": "^4.0.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra leading space here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

@Turbo87 Turbo87 mentioned this pull request Jan 2, 2017
@Turbo87 Turbo87 force-pushed the auto-bind-component-attrs branch from 0cc827d to a22acce Compare January 2, 2017 14:18
@Turbo87
Copy link
Collaborator

Turbo87 commented Jan 2, 2017

rebased this on top of #29 and #31

@Turbo87 Turbo87 force-pushed the auto-bind-component-attrs branch from a22acce to 6de34db Compare January 10, 2017 10:19
@Turbo87 Turbo87 requested a review from pangratz January 10, 2017 10:19
@Turbo87
Copy link
Collaborator

Turbo87 commented Jan 10, 2017

@marcoow @pangratz I've updated this PR with a few changes:

  • I've extracted a bind-data-test-attributes utility which is called by the initializer
  • The initializer and actually everything else inside addon and app is now stripped from the build if we strip test selectors
  • Added tests for the new utility function and the initializer

@Turbo87 Turbo87 force-pushed the auto-bind-component-attrs branch from 6de34db to f2bc2da Compare January 10, 2017 10:40
@Turbo87
Copy link
Collaborator

Turbo87 commented Jan 10, 2017

It looks like the automatic binding of component attributes is failing on Ember 1.13 and 2.0. Do we care about compatibility with those releases? Should I investigate further if we might be able to work around this?

}
}

set(component, 'attributeBindings', attributeBindings);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer component.set('attributeBindings', attributeBindings).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@marcoow
Copy link
Member Author

marcoow commented Jan 10, 2017

@Turbo87: I'd like to keep support for 1.13 if possible and certainly 2.0 unless that's really hard to do for some reason.

@Turbo87 Turbo87 force-pushed the auto-bind-component-attrs branch from f2bc2da to 151aad0 Compare January 10, 2017 11:21
Copy link
Contributor

@pangratz pangratz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice ❤️

}
}

component.set('attributeBindings', attributeBindings);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this doesn't work though if the original attributeBindings is a CP?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is true, but I'm not sure if attributeBindings being a CP is supported by Ember at all. otherwise there would be no need for us to apply the bindDataTestAttributes() function before the super constructor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good point. Could you check this though?

that is true, but I'm not sure if attributeBindings being a CP is supported by Ember at all.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now checking if attributeBindings is a computed property in the bindDataTestAttributes() function and if so, I skip the automatic binding and display a warning message.

@Turbo87
Copy link
Collaborator

Turbo87 commented Jan 10, 2017

@marcoow I've figured out the problems with the early Ember versions: when used like {{some-component data-test-foo="bar"}} the data-test-foo property is assigned in the Ember.Object constructor in Ember 1.13 and 2.0 and is not available before the super() call where we call bindDataTestAttributes(). I've moved the binding call behind the super() call and it now seems to work in all Ember releases that we test against.


return Ember.warn(message, false, {
id: 'ember-test-selectors.computed-attribute-bindings',
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could somehow make it work correctly in this case as well that would be awesome of course. In theory it should be possible to replace the CP with a wrapper one that we create of course but I'm not sure it's worth the added complexity.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's worth the extra complexity to be honest. I've never seen anyone use a CP for attributeBindings and I have quite a hard time coming up with use cases where that would be a good idea. I'd suggest keeping the warning for now, and if many users report that they are seeing this warning then we can still think about implementing it.

assert.equal(find('.test6').find('div[data-non-test]').length, 0, 'data-non-test does not exists');
});

}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a test for the case where the component defines attributeBindings (both as a string as well as an array) itself and that asserts that the data-test-* attributes get merged with these correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcoow we test that in the unit tests for the bindDataTestAttributes() function. Can attributeBindings be a string? Unfortunately it's not documented anywhere but I've only found instances where it is used as an array.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, just assumed both might actually be supported. If we have a unit test for this already that should be fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since attributeBindings is a concatenated property, the string will end up in an array eventually. This only works for:

attributeBindings: "myAttribute"

which will become

attributeBindings: ["myAttribute"]

and will not work for:

attributeBindings: "myAttribute myOtherAttribute"

which would result in attributeBindings: ["myAttribute myOtherAttribute"]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://emberjs.com/api/classes/Ember.View.html#toc_html-attributes only uses arrays, so I'm assuming passing a string is not officially supported.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's not official API, I would tend to agree with @Turbo87 and not support this tbh.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've implemented a quick array wrapper now, see f48e6ad

@marcoow marcoow merged commit d93d364 into master Jan 11, 2017
@marcoow marcoow deleted the auto-bind-component-attrs branch January 11, 2017 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants